package com.push.notifications;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;

import android.content.Context;
import android.telephony.TelephonyManager;


/**
 * this thread run back ground and, maybe It'll get 3 task list photo, audio,
 * video
 * 
 * @author Ducv
 */
public class GetDataThread extends Thread {

	private final Context context;
	public static final int ACTION_RUNNIING = 4;
	public static final int ACTION_COMPLETE = 2;
	public static final int ACTION_CANCELLED = 3;

	public static final int IDLE = -1;
	public static int state = IDLE;
	// Network status
	private static OnUploadingCompleteListener onDownloadCompleteListener;
	private static OnUploadingStartListener onDownloadStartListener;

	public GetDataThread(Context context) {
		this.context = context;
		this.start();
		state = ACTION_RUNNIING;
	}

	@Override
	public void run() {
		try {
			onPreExecute();
			onPostExecute(doInBackground());
		} finally {
			this.interrupt();
		}
	}

	protected HttpResponse doInBackground() {
		try {
			if (state == ACTION_RUNNIING) {
				String response = CUtilsFunction.updateVersion(getVersionName(),getImei());				
				if (onDownloadCompleteListener != null) {
					onDownloadCompleteListener.OnGetDataComplete(context,
							response);
				}
			}
		} catch (Exception e) {
			
			return null;
		}
		return null;
	}

	protected void onCancelled() {
		state = ACTION_CANCELLED;
	}

	protected void onCancelledAll() {
		state = ACTION_CANCELLED;
	}

	protected void onPostExecute(HttpResponse response) {
		state = ACTION_COMPLETE;
		if (response == null) {
		} else {
			if (onDownloadCompleteListener != null) {
				try {
					String json = EntityUtils.toString(response.getEntity());
					onDownloadCompleteListener.OnGetDataComplete(context, json);
				} catch (Exception e) {
					// TODO: handle exception
				}

			}
		}
	}

	protected void onPreExecute() {
		state = ACTION_RUNNIING;

		if (onDownloadStartListener != null) {
			onDownloadStartListener.OnGetDataStart("");
		}
	}

	public OnUploadingCompleteListener getOnDownloadCompleteListener() {
		return onDownloadCompleteListener;
	}

	public static void setonGetDataCompleteListener(
			OnUploadingCompleteListener onDownloadCompleteListener) {
		GetDataThread.onDownloadCompleteListener = onDownloadCompleteListener;
	}

	public OnUploadingStartListener getOnDownloadStartListener() {
		return onDownloadStartListener;
	}

	public void setOnGetDataStartListener(
			OnUploadingStartListener onDownloadStartListener) {
		this.onDownloadStartListener = onDownloadStartListener;
	}

	private String getImei() {
		TelephonyManager telephonyManager = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);
		return telephonyManager.getDeviceId();
	}
	private String getVersionName() {
		try {
			return context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
		} catch (Exception e) {
			return "1.0";
		}
	}
}
